﻿//Table:S_EmailSendMX

#region Entity

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Entity;
using Platform.Configuration;
using Platform.Data;

namespace Entity
{
    /// <summary>
    ///     邮件发送明细
    /// </summary>
    [Serializable]
    public class S_EmailSendMX
    {
        private DateTime? m_d_caozuosj;

        private DateTime? m_d_sendtime;

        private int? m_i_sendemailxx;

        private int? m_i_sendstate;

        private int? m_i_sendtimes;

        private int? m_i_sid;

        private int? m_i_zhuangtai;
        private int m_id;

        private string m_s_buzhoubh;

        private string m_s_caozuor;

        private string m_s_emailattfile;

        private string m_s_emailcontent;

        private string m_s_emailsubject;

        private string m_s_receiveemailadd;

        private string m_s_sendremarks;

        /// <summary>
        ///     主键
        /// </summary>
        public int ID
        {
            get { return m_id; }
            set { m_id = value; }
        }

        /// <summary>
        ///     项目SID
        /// </summary>
        public int? I_SID
        {
            get { return m_i_sid; }
            set { m_i_sid = value; }
        }

        /// <summary>
        ///     步骤编号
        /// </summary>
        public string S_BuZhouBH
        {
            get { return m_s_buzhoubh; }
            set { m_s_buzhoubh = value; }
        }

        /// <summary>
        ///     备注
        /// </summary>
        public string S_SendRemarks
        {
            get { return m_s_sendremarks; }
            set { m_s_sendremarks = value; }
        }

        /// <summary>
        ///     发件人
        /// </summary>
        public int? I_SendEmailXX
        {
            get { return m_i_sendemailxx; }
            set { m_i_sendemailxx = value; }
        }

        /// <summary>
        ///     收件人地址
        /// </summary>
        public string S_ReceiveEmailAdd
        {
            get { return m_s_receiveemailadd; }
            set { m_s_receiveemailadd = value; }
        }

        /// <summary>
        ///     邮件主题
        /// </summary>
        public string S_EmailSubject
        {
            get { return m_s_emailsubject; }
            set { m_s_emailsubject = value; }
        }

        /// <summary>
        ///     邮件内容
        /// </summary>
        public string S_EmailContent
        {
            get { return m_s_emailcontent; }
            set { m_s_emailcontent = value; }
        }

        /// <summary>
        ///     附件路径
        /// </summary>
        public string S_EmailAttFile
        {
            get { return m_s_emailattfile; }
            set { m_s_emailattfile = value; }
        }

        /// <summary>
        ///     发送次数
        /// </summary>
        public int? I_SendTimes
        {
            get { return m_i_sendtimes; }
            set { m_i_sendtimes = value; }
        }

        /// <summary>
        ///     最后一次发送时间
        /// </summary>
        public DateTime? D_SendTime
        {
            get { return m_d_sendtime; }
            set { m_d_sendtime = value; }
        }

        /// <summary>
        ///     发送状态（0待确认是否发发送1确认发送还未发送2发送失败3发送成功）
        /// </summary>
        public int? I_SendState
        {
            get { return m_i_sendstate; }
            set { m_i_sendstate = value; }
        }

        /// <summary>
        ///     操作人
        /// </summary>
        public string S_CaoZuoR
        {
            get { return m_s_caozuor; }
            set { m_s_caozuor = value; }
        }

        /// <summary>
        ///     操作时间
        /// </summary>
        public DateTime? D_CaoZuoSJ
        {
            get { return m_d_caozuosj; }
            set { m_d_caozuosj = value; }
        }

        /// <summary>
        ///     状态（0正常）
        /// </summary>
        public int? I_ZhuangTai
        {
            get { return m_i_zhuangtai; }
            set { m_i_zhuangtai = value; }
        }
    }
}

#endregion

#region EntityProcess

namespace BLL
{
    public abstract class S_EmailSendMXProcess
    {
        #region Field

        protected string conn;

        #endregion

        #region Construction

        public S_EmailSendMXProcess()
        {
            conn = ConfigHelper.PlatformConnString;
        }

        #endregion

        #region public

        public bool InsertOrUpdate(S_EmailSendMX S_EmailSendMX, DbTransaction Transaction)
        {
            if (S_EmailSendMX.ID == 0)
                InsertS_EmailSendMX(S_EmailSendMX, Transaction);
            else
                UpdateS_EmailSendMX(S_EmailSendMX, Transaction);
            return S_EmailSendMX.ID == 0 ? false : true;
        }

        public bool DeleteS_EmailSendMX(int ID, DbTransaction Transaction)
        {
            var spName = "_DeleteS_EmailSendMX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = ID;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            return result == 0 ? false : true;
        }

        public bool DeleteS_EmailSendMXesDynamic(string WhereClause, DbTransaction Transaction)
        {
            var spName = "_DeleteS_EmailSendMXesDynamic";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = WhereClause;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            return result == 0 ? false : true;
        }

        public DataTable LeftJoin<EntityName>(string On, string WhereClause, string OrderByExpression,
            DbTransaction Transaction)
        {
            DataSet ds = null;
            if (Transaction == null)
                ds = SqlHelper.ExecuteDataset(conn, CommandType.Text,
                    "select * from S_EmailSendMX left join " + typeof(EntityName).Name + " on " + On +
                    (string.IsNullOrEmpty(WhereClause.Trim()) ? "" : " where " + WhereClause) + " " +
                    (string.IsNullOrEmpty(OrderByExpression.Trim()) ? "" : " order by " + OrderByExpression));
            else
                ds = SqlHelper.ExecuteDataset((SqlTransaction) Transaction, CommandType.Text,
                    "select * from S_EmailSendMX left join " + typeof(EntityName).Name + " on " + On +
                    (string.IsNullOrEmpty(WhereClause.Trim()) ? "" : " where " + WhereClause) + " " +
                    (string.IsNullOrEmpty(OrderByExpression.Trim()) ? "" : " order by " + OrderByExpression));
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }

        public S_EmailSendMX SelectS_EmailSendMX(int ID, DbTransaction Transaction)
        {
            var spName = "_SelectS_EmailSendMX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = ID;
            SqlDataReader rd = null;
            S_EmailSendMX _S_EmailSendMX = null;
            if (Transaction == null)
                rd = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                rd = SqlHelper.ExecuteReader((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            if (rd.Read())
                _S_EmailSendMX = PopulateS_EmailSendMX(rd);
            if (rd != null)
                rd.Close();
            return _S_EmailSendMX;
        }

        public DataTable SelectS_EmailSendMXesAll(DbTransaction Transaction)
        {
            var spName = "_SelectS_EmailSendMXesAll";
            DataSet ds = null;
            if (Transaction == null)
                ds = SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, spName);
            else
                ds = SqlHelper.ExecuteDataset((SqlTransaction) Transaction, CommandType.StoredProcedure, spName);
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }

        public DataTable SelectS_EmailSendMXesDynamic(string WhereClause, string OrderByExpression,
            DbTransaction Transaction)
        {
            var spName = "_SelectS_EmailSendMXesDynamic";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = string.IsNullOrEmpty(WhereClause.Trim()) ? "1=1" : WhereClause;
            storedParams[1].Value = OrderByExpression;
            DataSet ds = null;
            if (Transaction == null)
                ds = SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                ds = SqlHelper.ExecuteDataset((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }

        public DataTable SelectS_EmailSendMXesBySerialNumberID(int SerialNumberID, string OrderByExpression,
            DbTransaction Transaction)
        {
            return SelectS_EmailSendMXesDynamic("SerialnumberID=" + SerialNumberID, OrderByExpression, Transaction);
        }

        #endregion

        #region Private

        private int InsertS_EmailSendMX(S_EmailSendMX S_EmailSendMX, DbTransaction Transaction)
        {
            var spName = "_InsertS_EmailSendMX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = S_EmailSendMX.I_SID;
            storedParams[1].Value = S_EmailSendMX.S_BuZhouBH;
            storedParams[2].Value = S_EmailSendMX.S_SendRemarks;
            storedParams[3].Value = S_EmailSendMX.I_SendEmailXX;
            storedParams[4].Value = S_EmailSendMX.S_ReceiveEmailAdd;
            storedParams[5].Value = S_EmailSendMX.S_EmailSubject;
            storedParams[6].Value = S_EmailSendMX.S_EmailContent;
            storedParams[7].Value = S_EmailSendMX.S_EmailAttFile;
            storedParams[8].Value = S_EmailSendMX.I_SendTimes;
            storedParams[9].Value = S_EmailSendMX.D_SendTime;
            storedParams[10].Value = S_EmailSendMX.I_SendState;
            storedParams[11].Value = S_EmailSendMX.S_CaoZuoR;
            storedParams[12].Value = S_EmailSendMX.D_CaoZuoSJ;
            storedParams[13].Value = S_EmailSendMX.I_ZhuangTai;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            S_EmailSendMX.ID = (int) storedParams[14].Value;
            return result;
        }

        private bool UpdateS_EmailSendMX(S_EmailSendMX S_EmailSendMX, DbTransaction Transaction)
        {
            var spName = "_UpdateS_EmailSendMX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = S_EmailSendMX.ID;
            storedParams[1].Value = S_EmailSendMX.I_SID;
            storedParams[2].Value = S_EmailSendMX.S_BuZhouBH;
            storedParams[3].Value = S_EmailSendMX.S_SendRemarks;
            storedParams[4].Value = S_EmailSendMX.I_SendEmailXX;
            storedParams[5].Value = S_EmailSendMX.S_ReceiveEmailAdd;
            storedParams[6].Value = S_EmailSendMX.S_EmailSubject;
            storedParams[7].Value = S_EmailSendMX.S_EmailContent;
            storedParams[8].Value = S_EmailSendMX.S_EmailAttFile;
            storedParams[9].Value = S_EmailSendMX.I_SendTimes;
            storedParams[10].Value = S_EmailSendMX.D_SendTime;
            storedParams[11].Value = S_EmailSendMX.I_SendState;
            storedParams[12].Value = S_EmailSendMX.S_CaoZuoR;
            storedParams[13].Value = S_EmailSendMX.D_CaoZuoSJ;
            storedParams[14].Value = S_EmailSendMX.I_ZhuangTai;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            return result == 0 ? false : true;
        }

        private S_EmailSendMX PopulateS_EmailSendMX(IDataReader dr)
        {
            var S_EmailSendMX = new S_EmailSendMX();
            S_EmailSendMX.ID = dr["ID"] == DBNull.Value ? 0 : (int) dr["ID"];
            S_EmailSendMX.I_SID = dr["I_SID"] == DBNull.Value ? null : (int?) dr["I_SID"];
            S_EmailSendMX.S_BuZhouBH = dr["S_BuZhouBH"] == DBNull.Value ? null : (string) dr["S_BuZhouBH"];
            S_EmailSendMX.S_SendRemarks = dr["S_SendRemarks"] == DBNull.Value ? null : (string) dr["S_SendRemarks"];
            S_EmailSendMX.I_SendEmailXX = dr["I_SendEmailXX"] == DBNull.Value ? null : (int?) dr["I_SendEmailXX"];
            S_EmailSendMX.S_ReceiveEmailAdd =
                dr["S_ReceiveEmailAdd"] == DBNull.Value ? null : (string) dr["S_ReceiveEmailAdd"];
            S_EmailSendMX.S_EmailSubject = dr["S_EmailSubject"] == DBNull.Value ? null : (string) dr["S_EmailSubject"];
            S_EmailSendMX.S_EmailContent = dr["S_EmailContent"] == DBNull.Value ? null : (string) dr["S_EmailContent"];
            S_EmailSendMX.S_EmailAttFile = dr["S_EmailAttFile"] == DBNull.Value ? null : (string) dr["S_EmailAttFile"];
            S_EmailSendMX.I_SendTimes = dr["I_SendTimes"] == DBNull.Value ? null : (int?) dr["I_SendTimes"];
            S_EmailSendMX.D_SendTime = dr["D_SendTime"] == DBNull.Value ? null : (DateTime?) dr["D_SendTime"];
            S_EmailSendMX.I_SendState = dr["I_SendState"] == DBNull.Value ? null : (int?) dr["I_SendState"];
            S_EmailSendMX.S_CaoZuoR = dr["S_CaoZuoR"] == DBNull.Value ? null : (string) dr["S_CaoZuoR"];
            S_EmailSendMX.D_CaoZuoSJ = dr["D_CaoZuoSJ"] == DBNull.Value ? null : (DateTime?) dr["D_CaoZuoSJ"];
            S_EmailSendMX.I_ZhuangTai = dr["I_ZhuangTai"] == DBNull.Value ? null : (int?) dr["I_ZhuangTai"];
            return S_EmailSendMX;
        }

        private void AddRowNumber(DataTable dt)
        {
            dt.Columns.Add(new DataColumn("RowNumber", typeof(int)));
            for (var i = 0; i < dt.Rows.Count; i++)
                dt.Rows[i][dt.Columns.Count - 1] = i + 1;
        }

        #endregion
    }
}

#endregion